<?php
namespace Wechat\ORG;

class AutoChat {
	
	function __construct() {
		
	}
	
	function chat($keyword, $type) {
		switch ($type) {
			case 'xhj' :
				/* 小黄鸡(www.xiaohuangji.com)聊天 */
				$data    = array('para' => $keyword);
				$return = POST('http://www.xiaohuangji.com/ajax.php', $data);
				break;
			case 'sns' :
				/* ajaxsns聊天 */
				$url = 'http://api.ajaxsns.com/api.php';
				$get = array(
						'key' => 'free',
						'appid' => 0,
						'msg'   => urlencode($keyword)
				);
				$content = (array)json_decode(GET($url, $get));
				$return = str_replace('{br}', "\n", $content['content']);
				break;
			case 'simi' :
				$url = 'http://www.simsimi.com/func/req';
				$data = array(
						'msg' => $keyword,
						'lc'  => 'ch'
				);
				$json = (array)json_decode(GET($url, $data));
				$return = $json['response'];
				break;
			default: $return = '没有定义回答';
		}
		return $return;
	}
	
	function wether($city) {
		$weather_cityId = include (dirname(__FILE__) . '/weather_cityId.php');
		//城市名称对应的代码
		$reply = "";    //赋空值，避免出错
		//根据城市名取城市代码
		$city_name = $city;
		$c_name = $weather_cityId[$city_name];
		$url = "http://www.weather.com.cn/data/sk/".$c_name.".html";
		if(!empty($c_name)){
			$json=GET($url);
			$data = json_decode($json);
			if(empty($data->weatherinfo)){
				$reply = "抱歉，没有查到\"".$city_name."\"的天气信息！";
			} else {
				$reply  = "【".$data->weatherinfo->city."时时天气】\n";
				$reply .= "今日 ".$data->weatherinfo->time."发布\n";
				$reply .= "室外气温：".$data->weatherinfo->temp."℃\n";
				$reply .= $data->weatherinfo->WD.' : '.$data->weatherinfo->WS."\n";
				$reply .= "空气湿度：".$data->weatherinfo->SD;
				//$reply = "【".$data->weatherinfo->city."天气预报】\n".$c_name.$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."时发布"."\n实时天气\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n温馨提示：".$data->weatherinfo->index_d."\n明天\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n后天\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3;
			}
		} else {
			$reply = "抱歉，没有查到\"".$city_name."\"的天气信息！";
		}
		return $reply ;
	}
	
	function music($title) {
		$url = 'http://box.zhangmen.baidu.com/x';
		$prase = array(
				'op' => 12,
				'count' => 1,
				'title' => '你的眼神$$',
		);
		$obj = (array)simplexml_load_string(GET($url,$prase), 'SimpleXMLElement', LIBXML_NOCDATA);
		if ($obj['count'] == 0) return false;
		$urlobj = $obj['url'];
		shuffle($urlobj);
		$music  = (array)$urlobj[0];
		return substr($music['encode'], 0, strrpos($music['encode'],'/')+1).$music['decode'];
	}
}